﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ESF.SSO.Core.Entities;
using ESF.General;
using ESF.General.Entity;
using ESF.SSO.Core.Entities.BasicReferences;
using ESF.SSO.Core.DataAccess.BasicReferences;

#endregion


namespace ESF.SSO.Core.DataAccess
{
	public static class AdoLocation
	{
		#region Write
		
		public static void RegisterLocation (LocatedBasicReference pLocBasRef)
		{
			IDbCommand cmd;


			if (pLocBasRef != null && pLocBasRef.ID != null)
			{
				cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_DeleteBasicReferenceLocations");
			
				AdoESFSSO.W.AddIDParameter (cmd, "pBasicReferenceID", pLocBasRef);
				AdoESFSSO.W.Execute (cmd);
			}

			
			if (pLocBasRef.Location == null) { return; }

			pLocBasRef.Location.ID = AdoLocation.Register (pLocBasRef.Location);

			if (pLocBasRef.Location.ID == null) { return; }


			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_InsertBasicReference_Location");
					
			AdoESFSSO.W.AddIDParameter (cmd, "pBasicReferenceID", pLocBasRef);
			AdoESFSSO.W.AddIDParameter (cmd, "pLocationID", pLocBasRef.Location.ID);
					
			AdoESFSSO.W.Execute (cmd);
		}


		internal static int? Register (Location pLocation)
		{
			IDbCommand cmd;

			string countryTemp;

			BasicReference tempCity;


			if (pLocation == null) { return null; }			

			if (pLocation.ID == null)
			{
				// Si esta vacio no lo crea
				if (TextHelper.IsEmpty (pLocation.Address) &&
					TextHelper.IsEmpty (pLocation.CellPhoneNumber) &&
					TextHelper.IsEmpty (pLocation.PhoneNumber) &&
					DataHelper.IsEmptyRef (pLocation.City) &&
					DataHelper.IsEmptyRef (pLocation.Country)) 
				{
					return null;
				} 
				else
				{
					cmd = AdoESFSSO.W.CreateSPCommand ("[esf_sso].P_InsertLocation");
				
					AdoESFSSO.W.AddOutParameter (cmd, "pID", DbType.Int32); 
				}
			}
			else
			{
				cmd = AdoESFSSO.W.CreateSPCommand ("[esf_sso].P_UpdateLocation");
				
				AdoESFSSO.W.AddParameter (cmd, "pID", pLocation.ID);
			}

			if (!DataHelper.IsEmptyRef (pLocation.City))
			{
				pLocation.City.BasicReferenceType = new BasicReferenceType () { ID = (int) BasicReferenceTypes.City };
				tempCity = AdoBasicReference.Get (pLocation.City);

				if (tempCity != null)
				{
					pLocation.City = tempCity;
				}
			}

			if (!DataHelper.IsEmptyRef (pLocation.Country))
			{
				countryTemp = pLocation.Country.Name;

				pLocation.Country.BasicReferenceType = new BasicReferenceType () { ID = (int) BasicReferenceTypes.Country };
				pLocation.Country = AdoBasicReference.Get (pLocation.Country);

				if (pLocation.Country == null)
				{
					throw new GenericInfoException (string.Format ("No hay registrado un País con nombre {0}", countryTemp));
				}
			}
			
			AdoESFSSO.W.AddIDParameter (cmd, "pCityID", pLocation.City);
			AdoESFSSO.W.AddNameParameter (cmd, "pCityName", pLocation.City);
			AdoESFSSO.W.AddIDParameter (cmd, "pCountryID", pLocation.Country);
			AdoESFSSO.W.AddParameter (cmd, "pName", pLocation.Name);
			AdoESFSSO.W.AddParameter (cmd, "pPhoneNumber", pLocation.PhoneNumber);
			AdoESFSSO.W.AddParameter (cmd, "pCellPhoneNumber", pLocation.CellPhoneNumber);
			AdoESFSSO.W.AddParameter (cmd, "pAddress", pLocation.Address);
			
			AdoESFSSO.W.Execute (cmd);
			
			return AdoESFSSO.W.GetParameterValue <int> (cmd, "pID");
		}
		
		#endregion
		
		
		#region Read
		
		public static Location AdjustValues (Location pLocation)
		{
			string countryTempName;
			string cityTempName;
			
			
			if (pLocation == null) { return null; }
			
			
			cityTempName = pLocation.City.Name;
					
			if (TextHelper.IsEmpty (cityTempName))
			{
				pLocation.City = null;
			}
			else
			{					
				pLocation.City.BasicReferenceType = new BasicReferenceType () { ID = (int) BasicReferenceTypes.City };
				pLocation.City = AdoBasicReference.Get (pLocation.City);
				
				if (pLocation.City == null)
				{
					pLocation.City = new BasicReference () { Name = cityTempName };
				}
			}
			
			countryTempName = pLocation.Country.Name;
			
			if (TextHelper.IsEmpty (countryTempName))
			{
				pLocation.Country = null;
			}
			else
			{
				pLocation.Country.BasicReferenceType = new BasicReferenceType() { ID = (int)BasicReferenceTypes.Country };
				pLocation.Country = AdoBasicReference.Get(pLocation.Country);
				
				if (pLocation.Country == null)
				{
					throw new GenericInfoException (string.Format ("Revise el Nombre del País, \"{0}\" no corresponde a uno registrado", countryTempName));
				}
			}
			
			return pLocation;
		}


		public static List<Location> GetList(Person pPerson)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.R.CreateSPCommand ("esf_sso.P_GetPersonLocations");
			
			AdoESFSSO.R.AddIDParameter (cmd, "pPersonID", pPerson.ID);
			
			return AdoESFSSO.R.GetList <Location> (cmd, ConvertTo);
		}
		
		
		
		internal static Location ConvertTo (IDataReader pReader, string pPrefix = "", string pListLoading = "")
		{
			Location result;
		
		
			result = new Location ()
			{
				ID = DataHelper.GetNullableInt (pReader, pPrefix + "Loc_ID"),
				Address = DataHelper.GetString (pReader, pPrefix + "Loc_Address"),
				CellPhoneNumber = DataHelper.GetString (pReader, pPrefix + "Loc_CellPhoneNumber"),
				City = DataHelper.ConvertToBasicReference<BasicReference> (pReader, pPrefix + "Loc_City_"),
				Country = DataHelper.ConvertToBasicReference<BasicReference> (pReader, pPrefix + "Loc_Cnt_"),
				Name = DataHelper.GetString (pReader, pPrefix + "Loc_Name"),
				PhoneNumber = DataHelper.GetString (pReader, pPrefix + "Loc_PhoneNumber")
			};
			
			if (result.City == null || string.IsNullOrEmpty (result.City.Name))
			{
				result.City = new BasicReference () { Name = DataHelper.GetString (pReader, pPrefix + "Loc_CityName") };
			}
			
			return result;
		}
		
		#endregion
	}
}
